###
# @Author: szx
# @Date: 2021-12-12 14:34:28
 # @LastEditTime: 2021-12-12 16:29:01
# @Description:
 # @FilePath: /couchdb-hastings-3.1.1/search_clouseau/geojson_searchOfGeo.sh
###

# 创建表
curl -X PUT admin:adminpw@localhost:5984/city

sleep 1
# 创建搜索索引，具体创建格式参考官方文档：https://docs.couchdb.org/en/3.1.1/ddocs/search.html#ddoc-search
# 这里索引则是geojson.geometry.coordinates的两个数组进行索引
curl -X POST -H "Content-Type: application/json" -d '
{
  "_id": "_design/trymesoft",
  "indexes" : {
    "shelter_search": {
      "index" : "function(doc) { if(doc.type && doc.geometry.type=='"'Point'"') { index('"'lon'"',doc.geometry.coordinates[0],{'"'store'"':true}); index('"'lat'"',doc.geometry.coordinates[1],{'"'store'"':true}); } }"
    }
  }
}' admin:adminpw@localhost:5984/city

sleep 1
# 创建地理空间索引
curl -X POST -H "Content-Type: application/json" -d '
{
  "_id": "_design/SpatialView",
  "st_indexes" : {
    "shelter_positions": {
      "index" : "function(doc) { if (doc.geometry) { st_index(doc.geometry); } }"
    }
  }
}' admin:adminpw@localhost:5984/city

sleep 1
# 添加数据
curl -X POST -H "Content-Type: application/json" -d '
{
	"type": "Feature",
	"geometry": {
		"type": "Point",
		"coordinates": [59.123, 18.123]
	},
	"properties": {
		"name": "1"
	}
}
' admin:adminpw@localhost:5984/city

sleep 1
curl -X POST -H "Content-Type: application/json" -d '
{
	"type": "Feature",
	"geometry": {
		"type": "Point",
		"coordinates": [58.789, 17.789]
	},
	"properties": {
		"name": "2"
	}
}
' admin:adminpw@localhost:5984/city

sleep 1
# 添加一个polygon
curl -X POST -H "Content-Type: application/json" -d '
{
	"type": "Feature",
	"geometry": {
		"type": "Polygon",
		"coordinates": [[[58.889,17.889],[58.989,17.889],[58.989,17.989],[58.889,17.889]]]
	},
	"properties": {
		"name": "3"
	}
}
' admin:adminpw@localhost:5984/city

sleep 1
# 地理空间查询圆内的图形，包括polygon
echo 'GeoSpatial result:'
curl -X GET "admin:adminpw@localhost:5984/city/_design/SpatialView/_geo/shelter_positions?lat=18&lon=59&radius=100000&relation=contains&format=geojson"

sleep 1
# 查询空间属性，具体使用方法查看 https://docs.couchdb.org/en/3.1.1/ddocs/search.html#ddoc-search-geographical-searches
echo 'Search result:'
curl -X POST -H "Content-Type: application/json" -d '
{
    "q":"*:*",
    "sort":"<distance,lon,lat,59,18,km>"
}' admin:adminpw@localhost:5984/city/_design/trymesoft/_search/shelter_search
